#!/usr/bin/perl
#disk_format.cgi
use File::Basename;

require './diskman-lib.pl';

&ReadParse();

my ($disk,$index,$err_msg,$unit,$begin,$name,$sysvpart,$markpart);

$sysvpart=$hwinfo{'sysvpart'}? $hwinfo{'sysvpart'}: 1; 
$markpart=$hwinfo{'markpart'}? $hwinfo{'markpart'}: 6;

if(!$in{'disk'}) {
	$err_msg=$text{'err_cgiparam'};
	goto ERROR_OUT;
}

if($in{'disk'} =~ /disk(\d+)/ ) {
	$index=$1;
} else {
	$err_msg=$text{'err_cgiparam'};
	goto ERROR_OUT;
}

@dinfo=&get_diskindex();

$disk=&diskname2rdev(\@dinfo,$in{'disk'});

if(!$disk) {
	$err_msg=$text{'err_cgiparam'};
	goto ERROR_OUT;
}

@rdinfo = get_check_raid_info();
@diskinfo = get_diskinfo(\@rdinfo);



if($diskinfo[$index - 1]->{'volname'} =~ /volume(\d+)/){
	$rddev = "/dev/md".($1 + 2);
	#$err_msg = "$disk|$rddev";
	#goto ERROR_OUT;

	`/sbin/mdadm $rddev --remove $disk`;
}

$out = `/usr/local/sbin/mkfs.xfs -f $disk`;

#cm for gzy

open(DI,$config{'diskinfo'});
@out=<DI>;
close(DI);
if(!@out) {
	$err_msg=$text{'err_dfmt_read_diskinfo'};
	goto ERROR_OUT;
}

$out[$index-1] =~ s/unknown/well/;

open(DI,"> $config{'diskinfo'}");
print DI @out;
close(DI);

redirect("index.cgi");

ERROR_OUT:
&error_setup($text{'diskfmt_err_title'});
&error($err_msg);
